import 'package:audioplayers/audioplayers.dart';
import 'package:flutter_rider/generated/assets.dart';
import 'package:flutter_rider/generated/r.dart';

class _MediaItem {
  var player = AudioCache(prefix: "");
  String path;
  var isInit = false;
  _MediaItem(this.path);
}

class SoundKit {
  SoundKit._privateConstructor() {
    init();
  }
  static final SoundKit _instance = SoundKit._privateConstructor();

  static SoundKit get instance {
    return _instance;
  }

  final List<_MediaItem> _medias = [];
  var _isPlay = false;

  init() {
    _medias.add(_MediaItem(Assets.voiceVoiceNewWaybill));
    _medias.add(_MediaItem(Assets.voiceVoiceNewWaybillCancel));
    _medias.add(_MediaItem(Assets.voiceVoiceNewWaybillMission));
    _medias.add(_MediaItem(Assets.voiceVoiceLoading));
    _medias.add(_MediaItem(Assets.voiceVoiceUnloading));
    _medias.add(_MediaItem(Assets.voiceVoiceWarning));
  }

  _play(int index) {
    if (_isPlay) {
      return;
    }
    var item = _medias[index];
    if (!item.isInit) {
      item.isInit = true;
      item.player.load(item.path);
    }
    _isPlay = true;
    item.player.play(item.path).then((AudioPlayer player) => player.onPlayerCompletion.listen((event) {
      _isPlay = false;
    }));
  }

playNewOrder(int index) {
    _play(index);
  }
}